﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Accessor;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;

namespace ZLCMS.Web.Example
{
    public partial class AspNetPager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //使用CACHE保存查询的总结果，若没有此CACHE，则需设置总数
            object obj = Cache[GetType() + "totalOrders"];
            if (obj == null)
            {
                // 设置记录总数
                int totalOrders = GetPageCount();

                Cache[GetType() + "totalOrders"] = totalOrders;
                AspNetPager1.RecordCount = totalOrders;
            }
            else
            {
                AspNetPager1.RecordCount = (int)obj;
            }

            // 获取页面索引值，并设置CurrentPageIndex，不清楚为什么这里不设置CurrentPageIndex则该值一直为1
            // 这里设置若为非有效索引值，AspNetPager会自动修改为最接近的有效值
            if (Request[AspNetPager1.UrlPageIndexName] != null)
            {
                try
                {
                    AspNetPager1.CurrentPageIndex = int.Parse(Request[AspNetPager1.UrlPageIndexName]);
                }
                catch (System.Exception ex)
                {
                    AspNetPager1.CurrentPageIndex = 1;
                }
            }

            BindList(AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);

            Label1.Text = AspNetPager1.CurrentPageIndex.ToString();
            Label2.Text = AspNetPager1.PageSize.ToString();
            Label3.Text = AspNetPager1.RecordCount.ToString();
            Label4.Text = AspNetPager1.StartRecordIndex.ToString();
            Label5.Text = AspNetPager1.EndRecordIndex.ToString();
            Label6.Text = AspNetPager1.PageCount.ToString();
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            //IMessageAccessor messageAcc = new AccessorFactory().GetMessageAccessor();
            //ICriteria criteria = messageAcc.CreateCriteria();
            //criteria.AddOrder(Order.Desc("Id")).SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

            //SetCondition(criteria);

            //gv_default.DataSource = criteria.List();
            //gv_default.DataBind();
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="criteria">criteria</param>
        private void SetCondition(ICriteria criteria)
        {
            //if (tb_search_title.Text.Trim() != "")
            //{
            //    criteria.Add(Expression.Like("Title", "%" + tb_search_title.Text + "%"));
            //}

            //if (tb_search_content.Text.Trim() != "")
            //{
            //    criteria.Add(Expression.Like("Content", "%" + tb_search_content.Text + "%"));
            //}
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <returns></returns>
        protected int GetPageCount()
        {
            //IMessageAccessor messageAcc = new AccessorFactory().GetMessageAccessor();
            //ICriteria ctr = messageAcc.CreateCriteria();
            //SetCondition(ctr);
            //return (int)ctr.SetProjection(Projections.RowCount()).UniqueResult();
            return 288;
        }
    }
}
